package com.yupi.springbootinit.statemachine.mapping;

import com.yupi.springbootinit.statemachine.enums.OrderStateEnum;

import java.util.EnumMap;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * 订单状态映射工具
 * 统一 DB 存储状态 与 状态机引擎枚举 的双向映射与归一化。
 *
 * DB状态（sales_order.orderStatus）:
 * - CREATED
 * - PROCESS_CONFIRMED
 * - PLAN_ISSUED
 * - IN_PRODUCTION
 * - PENDING_INSPECTION
 * - INSPECTED
 * - COMPLETED
 *
 * 状态机引擎状态（OrderStateEnum）:
 * - CREATED
 * - ROUTING_PUBLISHED
 * - SCHEDULE_PUBLISHED
 * - PRODUCING
 * - INSPECTING
 * - COMPLETED
 *
 * 归并策略：
 * - PENDING_INSPECTION / INSPECTED -> INSPECTING
 */
public final class OrderStatusMapper {

    private static final Map<String, OrderStateEnum> DB_TO_ENGINE = new HashMap<>();
    private static final Map<OrderStateEnum, String> ENGINE_TO_DB = new EnumMap<>(OrderStateEnum.class);

    static {
        // DB -> Engine
        DB_TO_ENGINE.put("CREATED", OrderStateEnum.CREATED);
        DB_TO_ENGINE.put("PROCESS_CONFIRMED", OrderStateEnum.ROUTING_PUBLISHED);
        DB_TO_ENGINE.put("PLAN_ISSUED", OrderStateEnum.SCHEDULE_PUBLISHED);
        DB_TO_ENGINE.put("IN_PRODUCTION", OrderStateEnum.PRODUCING);
        DB_TO_ENGINE.put("PENDING_INSPECTION", OrderStateEnum.INSPECTING);
        DB_TO_ENGINE.put("INSPECTED", OrderStateEnum.INSPECTING);
        DB_TO_ENGINE.put("COMPLETED", OrderStateEnum.COMPLETED);

        // Engine -> DB（默认映射）
        ENGINE_TO_DB.put(OrderStateEnum.CREATED, "CREATED");
        ENGINE_TO_DB.put(OrderStateEnum.ROUTING_PUBLISHED, "PROCESS_CONFIRMED");
        ENGINE_TO_DB.put(OrderStateEnum.SCHEDULE_PUBLISHED, "PLAN_ISSUED");
        ENGINE_TO_DB.put(OrderStateEnum.PRODUCING, "IN_PRODUCTION");
        ENGINE_TO_DB.put(OrderStateEnum.INSPECTING, "PENDING_INSPECTION"); // 默认归一为待检
        ENGINE_TO_DB.put(OrderStateEnum.COMPLETED, "COMPLETED");
    }

    private OrderStatusMapper() {
    }

    /**
     * 将任意输入状态（DB取值或引擎枚举code/name）归一为引擎状态
     */
    public static OrderStateEnum toEngineState(String status) {
        if (status == null || status.trim().isEmpty()) {
            throw new IllegalArgumentException("status is blank");
        }
        String normalized = status.trim().toUpperCase(Locale.ROOT);

        // 优先匹配引擎枚举（code 与 name 等价，这里使用 name/code 都是大写）
        for (OrderStateEnum e : OrderStateEnum.values()) {
            if (normalized.equals(e.name()) || normalized.equals(e.getCode())) {
                return e;
            }
        }

        // 匹配DB取值
        OrderStateEnum mapped = DB_TO_ENGINE.get(normalized);
        if (mapped != null) {
            return mapped;
        }

        throw new IllegalArgumentException("Unknown status: " + status);
    }

    /**
     * 将引擎状态映射为DB默认存储值
     */
    public static String engineToDb(OrderStateEnum engineState) {
        String db = ENGINE_TO_DB.get(engineState);
        if (db == null) {
            throw new IllegalArgumentException("No DB mapping for engine state: " + engineState);
        }
        return db;
    }

    /**
     * 是否为已知DB状态
     */
    public static boolean isKnownDbStatus(String status) {
        if (status == null) return false;
        return DB_TO_ENGINE.containsKey(status.trim().toUpperCase(Locale.ROOT));
    }
}